package com.egao.common.module.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.egao.common.core.exception.ParameterException;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageParam;
import com.egao.common.module.system.constants.CollegeMajorConstants;
import com.egao.common.module.system.entity.Major;
import com.egao.common.module.system.mapper.MajorMapper;
import com.egao.common.module.system.service.MajorService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author pyx
 */
@Service
public class MajorServiceImpl extends ServiceImpl<MajorMapper, Major> implements MajorService {

    @Override
    public List<Major> listPage(PageParam<Major> page) {
        return baseMapper.listPage(page);
    }

    @Override
    public JsonResult insertMajor(Major major) {
        if (major == null) {
            throw new ParameterException("参数对象为空");
        }

        if (StrUtil.isBlank(major.getCollegeCode())) {
            throw new ParameterException("所属院系不能为空");
        }
        if (StrUtil.isBlank(major.getName())) {
            throw new ParameterException("名称不能为空");
        }
        if (StrUtil.isBlank(major.getCode())) {
            throw new ParameterException("编码不能为空");
        }
        if (major.getXl() == null) {
            throw new ParameterException("学历不能为空");
        }

        baseMapper.insert(major);
        return JsonResult.ok("添加成功");
    }

    @Override
    public JsonResult updateMajor(Major major) {
        if (major == null) {
            throw new ParameterException("参数对象为空");
        }

        if (StrUtil.isBlank(major.getCollegeCode())) {
            throw new ParameterException("所属院系不能为空");
        }
        if (StrUtil.isBlank(major.getName())) {
            throw new ParameterException("名称不能为空");
        }
        if (StrUtil.isBlank(major.getCode())) {
            throw new ParameterException("编码不能为空");
        }
        if (major.getXl() == null) {
            throw new ParameterException("学历不能为空");
        }

        baseMapper.updateById(major);
        return JsonResult.ok("修改成功");
    }

    @Override
    public List<Major> listAll(String collegeCode, Integer xl) {
        List<Major> list = baseMapper.selectList(new QueryWrapper<Major>()
                .eq(!StrUtil.isBlank(collegeCode), "college_code", collegeCode)
                .eq(xl != null,"xl", xl)
                .orderByAsc("code")
        );
        for (Major item : list) {
            String xlLabel = item.getXl() != null ? CollegeMajorConstants.MAJOR_XL_BK == item.getXl() ? "本科" : "研究生" : "";
            item.setName(item.getName() + "(" + item.getCode() + ")" + "(" + xlLabel + ")");
        }
        return list;
    }

    @Override
    public List<Major> listAllNoCode(String collegeCode, Integer xl) {
        return baseMapper.selectList(new QueryWrapper<Major>()
                .eq(!StrUtil.isBlank(collegeCode), "college_code", collegeCode)
                .eq(xl != null,"xl", xl)
                .orderByAsc("code")
        );
    }

    @Override
    public Major getByCode(String collegeCode, String code) {
        return baseMapper.selectOne(new QueryWrapper<Major>()
                .eq("college_code", collegeCode)
                .eq("code", code)
                .orderByAsc("code")
                .last("limit 1")
        );
    }

    @Override
    public Major getByName(String collegeCode, String name) {
        return baseMapper.selectOne(new QueryWrapper<Major>()
                .eq("college_code", collegeCode)
                .eq("name", name)
                .orderByAsc("code")
                .last("limit 1")
        );
    }
}
